package com.imoocdl.controller;

import com.imoocdl.enums.ResultEnum;
import com.imoocdl.exception.SellException;
import com.imoocdl.service.OrderService;
import com.imoocdl.service.PayService;
import com.lly835.bestpay.model.PayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 支付
 * Created by pc on 2018/7/20.
 */
@Controller
@Slf4j
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private PayService payService;
    @GetMapping("/create")
    public ModelAndView create(@RequestParam("orderId") String orderId, @RequestParam("returnUrl") String returnUrl
                                , Map<String,Object> map){
        if(orderService.findOne(orderId) == null){
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        //发起支付

        PayResponse payResponse = payService.create(orderService.findOne(orderId));
        map.put("payResponse",payResponse);
        map.put("returnUrl",returnUrl);
        return new ModelAndView("pay/create",map);
    }

    /**
     * 微信异步通知
     */
    @PostMapping("/notify")
    public ModelAndView notify(@RequestBody String notify){
        payService.notify(notify);
        //返回给微信处理结果
        return new ModelAndView("pay/success");
    }
}
